
package io.gitee.hefren.hhcache.command;

import io.gitee.hefren.hhcache.core.Command;
import io.gitee.hefren.hhcache.core.CommandEnum;
import io.gitee.hefren.hhcache.core.HhCache;
import io.gitee.hefren.hhcache.core.Reply;

import java.util.Arrays;

/**
 * @Date 2024/7/9
 * @Author lifei
 */
public class ZaddCommand implements Command {
    @Override
    public String name() {
        return CommandEnum.ZADD.name();
    }

    @Override
    public Reply<?> exec(HhCache cache, String[] args) {
        String key = getKey(args);
        double[] scores = toDouble(getHkeys(args));
        String[] values = getHvalues(args);
        int num = cache.zadd(key, scores, values);
        return Reply.integerReply(num);
    }

    private double[] toDouble(String[] items) {
        return Arrays.stream(items).mapToDouble(Double::parseDouble).toArray();
    }
}
